package com.hubstc.book_borrow.controller;

import com.hubstc.book_borrow.pojo.Reader;
import com.hubstc.book_borrow.pojo.Result;
import com.hubstc.book_borrow.service.ReaderService;
import com.hubstc.book_borrow.utils.JwtUtil;
import com.hubstc.book_borrow.utils.Md5Util;
import jakarta.validation.constraints.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/reader")
@Validated
public class ReaderController{

    @Autowired
    private ReaderService readerService;

    @PostMapping("/register")
    public Result register(@Pattern(regexp = "^[a-zA-Z0-9_]{5,16}$") String readerId, @Pattern(regexp = "^[a-zA-Z0-9_]{5,16}$") String password) {
        Reader r = readerService.findByReaderId(readerId);
        if (r == null) {
            int readerPer = 1;
            readerService.register(readerId, password, readerPer);
            return Result.success();
        } else {
            return Result.error("该用户已注册");
        }
    }

    @PostMapping("/login")
    public Result login(@Pattern(regexp = "^[a-zA-Z0-9_]{5,16}$") String readerId, @Pattern(regexp = "^[a-zA-Z0-9_]{5,16}$") String password) {
        Reader loginReader = readerService.findByReaderId(readerId);
        if (loginReader == null) {
            return Result.error("该用名错误");
        }
        String md5String = Md5Util.getMD5String(password);
        if (md5String.equals(loginReader.getPassword())) {
            Map<String, Object> claims = new HashMap<>();
            claims.put("readerId", loginReader.getReaderId());
            claims.put("password", loginReader.getPassword());
            claims.put("readerPer", loginReader.getReaderPer());
            String token = JwtUtil.genToken(claims);
            return Result.success(token);
        }
        return Result.error("密码错误");
    }

    @GetMapping("/readerInfo")
    public Result readerInfo(@RequestHeader(name = "token") String token) {
        Map<String, Object> claims = JwtUtil.parseToken(token);
        String readerId=(String) claims.get("readerId");
        Reader reader=readerService.findByReaderId(readerId);
        return Result.success(reader);
    }

    @PutMapping("/updateReader")
    public Result updateReader(@RequestBody @Validated Reader reader){
        readerService.updateReader(reader);
        return Result.success();
    }

    @PostMapping("/updatePwd")
    public Result updatePwd(@RequestBody Map<String,Object> params,@RequestHeader(name="token") String token){
        String oldPwd=(String) params.get("oldPwd");
        String rePwd=(String) params.get("rePwd");
        String newPwd=(String) params.get("newPwd");

        if (!StringUtils.hasLength(oldPwd)||!StringUtils.hasLength(newPwd)||!StringUtils.hasLength(rePwd)){
            return Result.error("参数不能为空");
        }

        Map<String,Object> claims=JwtUtil.parseToken(token);
        String readerId=(String) claims.get("readerId");
        Reader loginReader=readerService.findByReaderId(readerId);
        if (!loginReader.getPassword().equals(Md5Util.getMD5String(oldPwd))){
            return  Result.error("原密码填写不正确");
        }
        if (!rePwd.equals(newPwd)){
            return Result.error("两次密码不一致");
        }
        readerService.updatePwd(newPwd,readerId);
        return Result.success();
    }


}
